package com.runshu.runshuwulian.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.runshu.runshuwulian.bean.LoginBean;

import java.util.List;

public class UtilSharedPreference {
    private static String LOGIN_INFO_KEY = "login_info";
    private static String MYDOCTOR_INFO_KEY = "mydoctor";
    private static String IMG_URL_KEY = "imgurl";
    private static String USER_INFO_KEY = "user_info";
    private static String HIS_SEARCH_KEY = "his_search_key";
    private static String IS_FIRST = "isfirst1";
    private static String SPLSH_IMG = "splash_img";
    private static String NUM = "num";
    private static String IS_CS = "iscs";

    private static String PACKAGE_NAME="com.runshu.runshuwulian";
    public static void setIsCS(Context ctx, boolean isCs) {
        SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
        sp.edit().putBoolean( IS_CS, isCs ).commit();
    }

    public static boolean isCS(Context ctx) {
        SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
        boolean isfirst = sp.getBoolean( IS_CS, false );
        return isfirst;
    }



    public static void putNum(Context ctx, int num) {
        SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
        sp.edit().putInt( NUM, num ).commit();
    }

    public static int getNum(Context ctx) {
        SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
        return sp.getInt( NUM, 0 );
    }


    public static void clearLoginInfo(Context ctx) {
        SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
        sp.edit().remove( LOGIN_INFO_KEY ).commit();
    }
    public static LoginBean getLoginInfo(Context ctx) {
        SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
        String string = sp.getString( LOGIN_INFO_KEY, "" );
        LoginBean loginInfoEntity = null;
        if (!string.isEmpty()) {
            loginInfoEntity = new Gson().fromJson( string, LoginBean.class );
        }
        return loginInfoEntity;
    }
    public static void saveLoginInfo(Context ctx, LoginBean loginInfo) {
        SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
        if (loginInfo != null)
            sp.edit().putString( LOGIN_INFO_KEY, new Gson().toJson( loginInfo ) ).commit();
    }
    public static boolean isFirst(Context ctx) {
        SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
        boolean isfirst = sp.getBoolean( IS_FIRST, true );
        return isfirst;
    }

    public static void setIsFirst(Context ctx) {
        SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
        sp.edit().putBoolean( IS_FIRST, false ).commit();
    }

    ;


    public static void saveImgUrl(Context ctx, String ImgHost) {
        if (!TextUtils.isEmpty( ImgHost )) {
            SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
            sp.edit().putString( IMG_URL_KEY, ImgHost ).commit();
        }
    }



    /**
     * 保存搜索历史记录
     *
     * @param ctx
     * @param keyList
     */
    public static void saveHisSearchKey(Context ctx, List<String> keyList) {
        if (keyList == null || keyList.size() == 0) return;
// HashSet h = new HashSet( keyList );
//        keyList.clear();
//        List<String> saveStr = new ArrayList<>();
//        saveStr.addAll( h );
        SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
        sp.edit().putString( HIS_SEARCH_KEY, new Gson().toJson( keyList ) ).commit();
    }

    /**
     * 获取搜索历史记录
     *
     * @return
     */
    public static List<String> getHisSearchKey(Context ctx) {
        SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
        String string = sp.getString( HIS_SEARCH_KEY, "" );
        List<String> result = null;
        if (!TextUtils.isEmpty( string ))
            result = new Gson().fromJson( string, new TypeToken<List<String>>() {
            }.getType() );

        return result;
    }

    /**
     * 清空历史记录
     *
     * @param ctx
     */
    public static void cleaHisSearchKey(Context ctx) {
        SharedPreferences sp = ctx.getSharedPreferences( PACKAGE_NAME, Context.MODE_PRIVATE );
        sp.edit().remove( HIS_SEARCH_KEY ).commit();
    }



}
